package com.example.task1.ui.AllService.library;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.LibraryCommentBean;
import com.example.task1.bean.LibraryDetailBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class LibraryDetailActivity extends BaseActivity {

    private TextView tvName;
    private TextView tvTime;
    private ImageView image;
    private TextView tvContent;
    private RecyclerView rvList;
    private int id;
    private ImageView mapImage;
    private int pageNum = 1;
    private boolean isTrue = true;
    private LibraryCommentAdapter libraryCommentAdapter;
    private LinearLayoutManager linearLayoutManager;
    private EditText etInput;
    private Button btnSend;

    private void loadCommentList() {
        rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (libraryCommentAdapter.getItemCount() - 1 == linearLayoutManager.findLastVisibleItemPosition() && isTrue) {
                    isTrue = false;
                    initData();
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library_detail);
        initView();
        id = getIntent().getIntExtra("id", 0);
        setAdapter();
        initData();
        getComment();
        loadCommentList();
        send();
    }

    private void send() {
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = etInput.getText().toString();
                if (TextUtils.isEmpty(s)) {
                    etInput.setError("不能为空");
                } else {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("libraryId", id);
                    map.put("content", s);
                    Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/digital-library/library-comment", Api.getBody(map));
                    Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                        @Override
                        public void success(ApiResp data) {
                            etInput.setText("");
                            MyApplication.show("发送成功");
                        }

                        @Override
                        public void error(String msg) {

                        }
                    });
                }
            }
        });
    }

    private void setAdapter() {
        libraryCommentAdapter = new LibraryCommentAdapter(this);
        linearLayoutManager = new LinearLayoutManager(this);
        rvList.setLayoutManager(linearLayoutManager);
        rvList.setAdapter(libraryCommentAdapter);
    }

    private void getComment() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/digital-library/library-comment/list?libraryId=" + id + "&pageNum=" + pageNum + "&pageSize=10");
        Api.send(call, LibraryCommentBean.class, new CallBack<LibraryCommentBean>() {
            @Override
            public void success(LibraryCommentBean data) {
                List<Integer> newId = data.getData().parallelStream().map(LibraryCommentBean.DataDTO::getId).collect(Collectors.toList());
                List<Integer> oldId = libraryCommentAdapter.getData().parallelStream().map(LibraryCommentBean.DataDTO::getId).collect(Collectors.toList());
                if (!oldId.containsAll(newId)) {
                    isTrue = true;
                    libraryCommentAdapter.setData(data.getData());
                    pageNum++;
                } else {
                    MyApplication.show("没有更多数据了");
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void initData() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/digital-library/library/" + id);
        Api.send(call, LibraryDetailBean.class, new CallBack<LibraryDetailBean>() {
            @Override
            public void success(LibraryDetailBean data) {
                LibraryDetailBean.DataDTO rows = data.getData();
                tvContent.setText(rows.getDescription());
                tvName.setText(rows.getName());
                tvTime.setText(rows.getBusinessHours());
                MyApplication.glide(image, rows.getImgUrl());
                MyApplication.glide(mapImage, rows.getMapUrl());
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public String getMyTitle() {
        return "图书馆详情";
    }

    private void initView() {
        tvName = findViewById(R.id.tv_name);
        tvTime = findViewById(R.id.tv_time);
        image = findViewById(R.id.image);
        tvContent = findViewById(R.id.tv_content);
        rvList = findViewById(R.id.rv_list);
        mapImage = findViewById(R.id.map_image);
        etInput = findViewById(R.id.et_input);
        btnSend = findViewById(R.id.btn_send);
    }
}